﻿using System;
using System.Linq;
using VCards.Extensions;
using VCards.Types;

namespace VCards.Serializer;

public static class DefaultSerializer
{
    public static string GetVCardString(string key, string value, bool mustEscape, VCardVersion version, string type = "", string encoding = "")
    {
        string[] types = {type};
        return GetVCardString(key, value, mustEscape, version, types, encoding);
    }

    public static string GetVCardString(string key, string value, bool mustEscape, VCardVersion version, string[] types, string encoding = "")
    {
        if (string.IsNullOrWhiteSpace(key))
        {
            return string.Empty;
        }

        if (mustEscape)
        {
            value = value.Escape();
        }

        //Legal values for v3
        //"TYPE=dom;TYPE=postal"
        //or
        //"TYPE=dom,postal"
        string type = "TYPE=" + string.Join(",", types);

        if (version == VCardVersion.V2_1)
        {
            type = string.Join(";", types);
        }

        string line = key;


        if (types.Any(x => !string.IsNullOrWhiteSpace(x)))
        {
            line = line + ";" + type;
        }

        if (!string.IsNullOrWhiteSpace(encoding))
        {
            line = line + $";ENCODING={encoding}";
        }

        line = line + ":" + value + Environment.NewLine;
        return line;
    }
}